﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace __ts.Core.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    public static class Crypto
    {
        static Crypto(){ }

        //密钥
        private const string sKey = "(=_=)@duAT98Ci6whdhwVsSUo03CBK4xDdT8F383KkR";

        //构造一个对称算法
        private static SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
            byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public static string SHA1Encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
            byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public static string SHA256Encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA256Managed sha256hasher = new SHA256Managed();
            byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public static string SHA384Encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA384Managed sha384hasher = new SHA384Managed();
            byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public static string SHA512Encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA512Managed sha512hasher = new SHA512Managed();
            byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputArray"></param>
        /// <returns></returns>
        public static string ByteArrayToString(byte[] inputArray)
        {
            StringBuilder output = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                output.Append(inputArray[i].ToString("X2"));
            }
            return output.ToString();
        }

        ///// <summary>
        ///// 加密字符串
        ///// </summary>
        ///// <param name="Value">输入的字符串</param>
        ///// <returns>加密后的字符串</returns>
        //public static string Encrypt(string Value)
        //{
        //    ICryptoTransform ct;
        //    MemoryStream ms;
        //    CryptoStream cs;
        //    byte[] byt;
        //    mCSP.Key = Convert.FromBase64String(sKey);
        //    mCSP.IV = Convert.FromBase64String(sIV);
        //    //指定加密的运算模式
        //    mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
        //    //获取或设置加密算法的填充模式
        //    mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
        //    ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
        //    byt = Encoding.UTF8.GetBytes(Value);
        //    ms = new MemoryStream();
        //    cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
        //    cs.Write(byt, 0, byt.Length);
        //    cs.FlushFinalBlock();
        //    cs.Close();
        //    return Convert.ToBase64String(ms.ToArray());
        //}

        ///// <summary>
        ///// 解密字符串
        ///// </summary>
        ///// <param name="Value">加过密的字符串</param>
        ///// <returns>解密后的字符串</returns>
        //public static string Decrypt(string Value)
        //{
        //    ICryptoTransform ct;
        //    MemoryStream ms;
        //    CryptoStream cs;
        //    byte[] byt;
        //    mCSP.Key = Convert.FromBase64String(sKey);
        //    mCSP.IV = Convert.FromBase64String(sIV);
        //    mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
        //    mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
        //    ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
        //    byt = Convert.FromBase64String(Value);
        //    ms = new MemoryStream();
        //    cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
        //    cs.Write(byt, 0, byt.Length);
        //    cs.FlushFinalBlock();
        //    cs.Close();
        //    return Encoding.UTF8.GetString(ms.ToArray());
        //}

        /// <summary> 
        /// 使用缺省密钥字符串加密 
        /// </summary> 
        /// <param name="original">明文</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string original)
        {
            return Encrypt(original, sKey, Encoding.Default);
        }

        /// <summary> 
        /// 使用缺省密钥字符串加密 
        /// </summary> 
        /// <param name="original">明文</param>
        /// <param name="encoding"></param>
        /// <returns>密文</returns> 
        public static string Encrypt(string original, Encoding encoding)
        {
            return Encrypt(original, sKey, encoding);
        }

        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">原始文字</param> 
        /// <param name="key">密钥</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string original, string key)
        {
            return Encrypt(original, key, Encoding.Default);
        }

        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">原始文字</param> 
        /// <param name="key">密钥</param> 
        /// <param name="encoding">字符编码方案</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string original, string key, Encoding encoding)
        {
            byte[] buff = encoding.GetBytes(original);
            byte[] kb = encoding.GetBytes(key);
            return Convert.ToBase64String(Encrypt(buff, kb));
        }

        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">原始数据</param> 
        /// <returns>密文</returns> 
        public static byte[] Encrypt(byte[] original)
        {
            byte[] key = System.Text.Encoding.Default.GetBytes(sKey);
            return Encrypt(original, key);
        }

        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">明文</param> 
        /// <param name="key">密钥</param> 
        /// <returns>密文</returns> 
        public static byte[] Encrypt(byte[] original, byte[] key)
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.Key = MakeMD5(key);
            des.Mode = CipherMode.ECB;

            return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
        }

        /// <summary> 
        /// 使用缺省密钥解密 
        /// </summary> 
        /// <param name="original">密文</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string original)
        {
            return Decrypt(original, sKey, System.Text.Encoding.Default);
        }
        /// <summary> 
        /// 使用给定密钥解密 
        /// </summary> 
        /// <param name="original">密文</param> 
        /// <param name="key">密钥</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string original, string key)
        {
            return Decrypt(original, key, System.Text.Encoding.Default);
        }
        /// <summary> 
        /// 使用缺省密钥解密,返回指定编码方式明文 
        /// </summary> 
        /// <param name="original">密文</param> 
        /// <param name="encoding">编码方式</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string original, Encoding encoding)
        {
            return Decrypt(original, sKey, encoding);
        }

        /// <summary> 
        /// 使用给定密钥解密 
        /// </summary> 
        /// <param name="encrypted">密文</param> 
        /// <param name="key">密钥</param> 
        /// <param name="encoding">字符编码方案</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string encrypted, string key, Encoding encoding)
        {
            byte[] buff = new byte[] { };
            try
            {
                buff = Convert.FromBase64String(encrypted);
            }
            catch { }
            byte[] kb = System.Text.Encoding.Default.GetBytes(key);
            return encoding.GetString(Decrypt(buff, kb));
        }


        /// <summary> 
        /// 使用缺省密钥解密数据 
        /// </summary> 
        /// <param name="encrypted">密文</param> 
        /// <returns>明文</returns> 
        public static byte[] Decrypt(byte[] encrypted)
        {
            byte[] key = System.Text.Encoding.Default.GetBytes(sKey);
            return Decrypt(encrypted, key);
        }

        /// <summary> 
        /// 使用给定密钥解密数据 
        /// </summary> 
        /// <param name="encrypted">密文</param> 
        /// <param name="key">密钥</param> 
        /// <returns>明文</returns> 
        public static byte[] Decrypt(byte[] encrypted, byte[] key)
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.Key = MakeMD5(key);
            des.Mode = CipherMode.ECB;
            try
            {
                return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
            }
            catch
            {
                return new byte[] { };
            }
        }

        /// <summary> 
        /// 生成MD5摘要 
        /// </summary> 
        /// <param name="original">数据源</param> 
        /// <returns>摘要</returns> 
        public static byte[] MakeMD5(byte[] original)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            byte[] keyhash = hashmd5.ComputeHash(original);
            hashmd5 = null;
            return keyhash;
        }

        /// <summary>
        /// MD5非对称加密
        /// </summary>
        /// <param name="times">加密次数</param>
        /// <param name="text">要加密的文本</param>
        /// <returns>加密后的文本</returns>
        public static string MD5Encrypt(int times, string text)
        {
            for (int num1 = 0; num1 < times; num1++)
            {
                text = FormsAuthentication.HashPasswordForStoringInConfigFile(text, "md5");
            }
            return text;
        }

    }
}
